<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 输入框 绑定queryInfo中的query属性 调用获取商品列表的函数 -->
        <el-col :span="14" class="order_col">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格(元)" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="order_pay">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === 1">已付款</el-tag>
            <el-tag type="danger" else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <!-- 用时间过滤器 需要用作用域插槽的形式 -->
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 操作按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="small" @click="showAddressDialog"></el-button>
            <!-- 物流信息按钮 -->
            <el-button
              type="success"
              icon="el-icon-location-outline"
              size="small"
              @click="showLogisticsDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="dialogVisible"
      width="50%"
      @close="editAddressFormClosed"
    >
      <!-- form表单 -->
      <el-form
        :model="editAddressForm"
        status-icon
        :rules="editAddressFormRules"
        ref="editAddressFormForm"
        label-width="100px"
      >
        <el-form-item label="省/市/区/县" prop="address1">
          <!-- options是数据源 v-model选中的值和谁双向绑定 -->
          <el-cascader
            v-model="editAddressForm.address1"
            :options="cityData"
            :props="{ checkStrictly: true,expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 位置信息对话框 -->
    <el-dialog title="修改地址" :visible.sync="logisticsDialogVisible" width="50%">
      <!-- timeline时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
import { get } from 'http'
export default {
  data() {
    return {
      queryInfo: {
        query: '', //输入的查询内容
        pagenum: 1, // 当前页码
        pagesize: 10, //显示条数
        user_id: '',
        pay_status: '', //支付状态
        is_send: '', //是否发货
        order_fapiao_title: '', //个人||公司
        order_fapiao_company: '', //公司名称
        order_fapiao_content: '', //发票内容
        consignee_addr: '' //发货地址
      },
      //获取到的订单列表数据
      orderList: [],
      total: 0,
      dialogVisible: false,
      //表单的数据对象
      editAddressForm: {
        address1: '',
        address2: ''
      },
      //表单的验证规则对象
      editAddressFormRules: {
        address1: [{ required: true, message: '请输入省/市/区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      cityData, //属性名和属性值重名属性值可以省略
      //   默认关闭物流信息对话框

      logisticsDialogVisible: false,
      //物流信息
      logisticsList: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    //获取订单数据列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.message.error('获取失败')
      }
      console.log(res)
      this.orderList = res.data.goods
      this.total = res.data.total
      //    console.log(this.orderList)
    },
    //监听分页显示条数的变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      //赋值之后 重新获取列表
      this.getOrderList()
    },
    //监听当前的页码值变化
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getOrderList()
    },
    //点击显示修改地址对话框
    showAddressDialog() {
      this.dialogVisible = true
    },
    //监听修改地址的关闭事件
    editAddressFormClosed() {
      this.$refs.editAddressFormForm.resetFields()
    },
    //点击显示物流信息对话框
    async showLogisticsDialog() {
      //展示对话框之前要先获取物流数据
      const { data: res } = await this.$http.get('kuaidi/itcast-g7kmck725jaujgdts')
      if (res.meta.status !== 200) {
        return this.message.error('获取物流信息失败')
      }
      this.logisticsList = res.data
      console.log(this.logisticsList)
      this.logisticsDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../plugins/timeline-item/timeline-item.css';
@import '../../plugins/timeline/timeline.css';
.order_col {
  margin-bottom: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
